完美解析C语言函数puts()、free()和gets()

您所在的位置:网站首页 puts gets的头文件 完美解析C语言函数puts()、free()和gets()

完美解析C语言函数puts()、free()和gets()

2024-07-15 13:59| 来源: 网络整理| 查看: 265

1、 puts

puts() 只用来输出字符串,就算只有一个字母“a” ,也会当成一个字符串。因为字符串的最后一项默认为 “\0” (字符串终止符的标志),而puts()遇到 \0 会输出 \n!!!也就是说,puts(s) 和 printf("%s\n",s) 的输出格式是一样的。printf()的输出格式很多,可以根据不懂得需要加转义字符,达到格式化输出。

puts()的参数可以直接是字符串,如puts(“abcd”); 也可以是变量名, 如 a = “abcd”; puts(a); 有换行

printf()的参数也可以直接是字符串,如printf(“abcd”);要实现换行必须printf("%s\n",a);

用法 int puts(const char *string)

puts()函数包含在头文件中

实例 1、输出字符串数组

#include int main(){ char str[]="hello world"; puts(str); return 0; }

/*output: hello world 按任意键退出 */ 注意输出的“hello world”后面有一个换行。

2、从指定字符位置开始输出

#include int main(){ char str[]="hello world"; puts(str+2); return 0; }

/*output: llo world 按任意键退出 */ 说明 1、puts()只能输出字符串,不能输出数值或者进行格式转换,即不能要求输出格式增加空格、换行(指的是输出内容的中间进行换行)等要求;

2、可以将字符串直接写入puts()。如:puts(“hello world”);

3、puts()和 printf的用法一样,puts()函数的作用与语句“printf("%s\n",s);的作用相同。注意:puts在输出字 符串后会自动输出一个回车符。

4、puts()函数的一种实现方案如下:

int puts(const char * string)  {  const char * t = string;  const char * v = string;  int i = 0;  while(*t!='\0')  {  i++;  t++;  }  int j = 0;  for(j;j


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3